﻿using Ellanet.Classes;
using Ellanet.ViewModels;
using Ellanet.Wrappers;
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Ellanet.Windows
{
    public partial class SelectionWindow
    {
        public SelectionWindowViewModel Vm;

        public SelectionWindow()
        {
            InitializeComponent();
            Vm = new SelectionWindowViewModel();
            Vm.RequestViewClose += Vm_RequestViewClose;
            DataContext = Vm;
        }

        private void Vm_RequestViewClose()
        {
            Close();
        }

        private void SelectionWindow_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                try
                {
                    DragMove();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }

        private void CloseButton_OnClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void VoiceRateButton_OnClick(object sender, RoutedEventArgs e)
        {
            var button = (Button) sender;

            if (!button.ContextMenu.IsOpen)
            {
                InputManager.Current.ProcessInput(new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Right) {RoutedEvent = MouseUpEvent, Source = sender});
            }
        }

        private void VoiceRateMenuItemEvent_OnHandler(object sender, RoutedEventArgs e)
        {
            var mi = (MenuItem) sender;

            foreach (var vr in Vm.VoiceRates.Where(vr => vr.IsSelected))
            {
                vr.IsSelected = false;
            }

            Vm.VoiceRates.First(vr => vr.Name.Equals(mi.Header)).IsSelected = true;
            SpeechWrapper.SayAsync(mi.Header.ToString(), false);
        }

        private void VoiceNameButton_OnClick(object sender, RoutedEventArgs e)
        {
            var button = (Button) sender;

            if (!button.ContextMenu.IsOpen)
            {
                InputManager.Current.ProcessInput(new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, MouseButton.Right) {RoutedEvent = MouseUpEvent, Source = sender});
            }
        }

        private void VoiceNameMenuItemEvent_OnHandler(object sender, RoutedEventArgs e)
        {
            var mi = (MenuItem) sender;

            foreach (var vn in Vm.VoiceNames.Where(vr => vr.IsSelected))
            {
                vn.IsSelected = false;
            }

            Vm.VoiceNames.First(vn => vn.Name.Equals(mi.Header)).IsSelected = true;
            SpeechWrapper.SayAsync(mi.Header.ToString(), false);
        }

        private void MultiplierSelected_OnHandler(object sender, RoutedEventArgs e)
        {
            var lbi = (ListBoxItem) sender;

            if (lbi.IsSelected)
            {
                SpeechWrapper.SayAsync(((Multiplicand) lbi.Content).Value.ToString(), false);
            }
        }

        private void TitleImage_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                Process.Start("https://minionmaths.codeplex.com/");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }

        private void UpdateCallout_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                Process.Start("https://minionmaths.codeplex.com/releases");
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw;
            }
        }
    }
}
